open(BUFF,"sort_cut_out_canonical_cds_lnc_minus") or die "no file sort_cut_out_canonical_cds_lnc_minus found\n";
open(WRBUFF,">out_cut_out_canonical_cds_lnc_minus") or die "not able to write to output file\n";;
#$line = <BUFF>;
while($line=<BUFF>)
{
	chomp($line);
	@a = split(/\t/,$line);
	if($a[0] !~ m/_/)
	{
		
		$first = int($a[1]/50)*50;
		$second = int($a[2]/50)*50+50;
		if(int($a[2]/50) == $a[2]/50)
		{
			$second = int($a[2]/50)*50;
		}
		for($i=$first;$i<$second;$i=$i+50)
		{
			$m = $i+1;
			$n = $i+50;
			print WRBUFF $a[0].'_'.$m.'_'.$n."\t".$a[4]."\n";
		}
	}
}
system(" sort -t '_' -k1,1 -k2,2n out_cut_out_canonical_cds_lnc_minus > out_cut_out_canonical_cds_lnc_minus1");
system("mv out_cut_out_canonical_cds_lnc_minus1 out_cut_out_canonical_cds_lnc_minus");
open(BUFF,"out_cut_out_canonical_cds_lnc_minus") or die "no file found out_cut_out_canonical_cds_lnc_minus\n";
open(WRBUFF,">out_cut_out_canonical_cds_lnc_minus1") or die "no file found out_cut_out_canonical_cds_lnc_minus1\n";
@a = ();
$prev = "";
$k = "";
while($line=<BUFF>)
{
        chomp($line);
	@a = split("\t",$line);
	
	if($prev ne $a[0])
	{
		if($prev ne "")
		{
			print WRBUFF $prev."\t".$k."\n";
		}
		$k = $a[1];		
	}
	else
	{
		$k = $k.','.$a[1];
	}
	$prev = $a[0];
}
system("mv out_cut_out_canonical_cds_lnc_minus1 out_cut_out_canonical_cds_lnc_minus");
